/**
 * @file Vector.java
 *
 * @author Usuario
 * @version
 * @date 17/12/2010
 */
package linear;

/**
 * @author Usuario
 *
 */
public class Vector {
	
	private int dim;
	private double[] elems;
	
	public Vector(int dim){
		elems = new double[dim];
		this.dim = dim;
	}
	
	public Vector copy(){
		Vector v = new Vector(dim);
		for(int i =0; i < dim; i++){
			v.elems[i] = elems[i];
		}
		return v;
	}
	
	public long getDim(){
		return dim;
	}
	
	public Vector normalize(){
		Vector v = new Vector(dim);
		double mod = modulo();
		for(int i =0; i < dim; i++){
			v.elems[i] = elems[i]/mod;
		}
		return v;
	}
	
	public double modulo(){
		double value = 0.0;
		for(int i =0; i < dim; i++){
			value += elems[i]*elems[i];
		}
		return Math.sqrt(value);	
	}
	
	public Vector sum(Vector v){
		Vector u = new Vector(dim);
		for(int i =0; i < dim; i++){
			u.elems[i] = elems[i] + v.elems[i];
		}
		return u;
	}
	
	public Vector sub(Vector v){
		Vector u = new Vector(dim);
		for(int i =0; i < dim; i++){
			u.elems[i] = elems[i] - v.elems[i];
		}
		return u;
	}
	
	public Vector multi(double k){
		Vector u = new Vector(dim);
		for(int i =0; i < dim; i++){
			u.elems[i] = k*elems[i];
		}
		return u;
	}
	
	public Vector div(double k){
		Vector u = new Vector(dim);
		for(int i =0; i < dim; i++){
			u.elems[i] = elems[i]/k;
		}
		return u;
	}
	
	public double escalarProduct(Vector v){
		double value = 0.0;
		for(int i =0; i < dim; i++){
			value += elems[i]*v.elems[i];
		}
		return value;
	}
	
	public double angleBetween(Vector v){
		return Math.acos(escalarProduct(v));
	}
	
	public boolean isOrtogonal(Vector v){
		return angleBetween(v) == Math.PI/2;
	}
	
}
